package com.group.controller;

import com.group.entity.SRT;
import com.group.entity.Solution;
import com.group.entity.TimeMove;
import com.group.view.MainView;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author zhanglianyong
 * 2022/12/23 16:43
 */
public class PlaceTextController {

    private Solution solution;
    private MainView view;
    private TimeMove timeMove;

    public PlaceTextController(Solution solution, MainView mainView, TimeMove timeMove) {
        this.solution = solution;
        this.view = mainView;
        this.timeMove = timeMove;
    }

    public void controller() {
        // 字幕
        TreeMap<Integer, SRT> srtMap = solution.getSrtMap();
        Long startMillSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        Set<Integer> set = srtMap.keySet();
        int index = 0;
        SRT srt;
        while (index < set.size()) {
            srt = srtMap.get(index);
            Long nowMillSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            Integer beginTime = srt.getBeginTime();
            Integer endTime = srt.getEndTime();
            // 加上时间偏移
            long diff = nowMillSecond - startMillSecond + timeMove.getMilliSecond();
            if (diff < beginTime) {
                continue;
            }
            String srtBody = srt.getSrtBody();
            // 更新字幕
            if (diff >= beginTime && diff <= endTime) {
                view.setLabel(srtBody);
            }
            if (diff <= endTime) {
                continue;
            }
            view.setLabel("");
            index++;
        }
    }


}
